/****************************************************************************
**
** Copyright (C) 2009 Alexander Solis.
**
** This file is part of Alejandria
** Alejandria is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Alejandria is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Alejandria.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include <QtGui>

#include "categorydelegate.h"

CategoryDelegate::CategoryDelegate(QObject *parent)
	: QItemDelegate(parent)
{

}

QWidget *CategoryDelegate::createEditor (QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
	QComboBox *editor = new QComboBox(parent);
	
	// Loads book cathegories according to Dewey Decimal classes book classification
    QStringList comboData;
    comboData << "" << "Computer science, information & general works"  << "Philosophy and psychology"  << "Religion"  << "Social sciences"  
        << "Language"  << "Science"  << "Technology"  << "Arts and recreation"  << "Literature"  << "History, geography and biography";
    editor->addItems(comboData);
	
	return editor;
}

void CategoryDelegate::setEditorData (QWidget *editor, const QModelIndex &index) const
{
	QString value = index.model()->data(index, Qt::EditRole).toString();
	
	QComboBox *comboBox = static_cast<QComboBox*>(editor);
	comboBox->setCurrentIndex(comboBox->findText(value));
}

void CategoryDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	QComboBox *comboBox = static_cast<QComboBox*>(editor);
	QString value = comboBox->currentText();
		
	model->setData(index, value, Qt::EditRole);
}

void CategoryDelegate::updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}